package day6;

import org.junit.Test;

import java.util.*;

public class CollectionDemo {
    // 1、在同一个包下面的类是不需要import过来就可以直接使用的
    // 2、java.lang包下面的所有的类也不需要import可以直接使用
    @Test
    public void test1() {
        Student student1 = new Student(1, "张三1", 23);
        Student student2 = new Student(2, "张三2", 23);
        Student student3 = new Student(3, "张三3", 23);
        Student student4 = new Student(4, "张三4", 23);
        // 数组最大的问题是数组长度固定
        Student[] array = new Student[3];
        array[0] = student1;
        array[1] = student2;
        array[2] = student3;
        for (int i = 0; i < array.length; i++) {
            System.out.println(array[i]);
        }
        System.out.println("---------------");
        for (Student student : array) {
            System.out.println(student);
        }

        // ArrayList<Student> list = new ArrayList<>();
        List<Student> list = new ArrayList<>();
        list.add(student1);
        list.add(student2);
        list.add(student3);
        list.add(student4);
        list.add(student4);
        System.out.println("---------------");
        // 增强的for循环
        for (Student student : list) {
            System.out.println(student);
        }
        System.out.println("---------------");
        for (int i = 0; i < list.size(); i++) {
            Student student = list.get(i);
            System.out.println(student);
        }
        System.out.println("-----------------");
        //HashSet<Student> set = new HashSet<>();
        Set<Student> set = new HashSet<>();
        set.add(student1);
        set.add(student2);
        set.add(student3);
        set.add(student4);
        set.add(student4);
        for (Student student : set) {
            System.out.println(student);
        }
    }

    @Test
    public void test2() {
        HashMap<String, String> map = new HashMap();
        map.put("CN", "中华人民共和国");
        map.put("US", "坚国");
        // 可以根据key，找到这个key对应的value值
        String value = map.get("US");
        System.out.println(value);
        // Map遍历 EntrySet
        Set<Map.Entry<String, String>> entrySet = map.entrySet();
        for (Map.Entry<String, String> entry: entrySet) {
            System.out.println(entry.getKey() + " : " + entry.getValue());
        }
        // KeySet
        Set<String> set = map.keySet();
        for (String key : set) {
            System.out.println(key + " : " + map.get(key));
        }
    }

}
